<?php

class Default_Model_CategoryItem extends Zend_Db_Table_Abstract
{
    protected $_name = 'category_item';
    public function getAllData(){
        $query = $this->select();
        $query->from($this->_name, array('categoryItemId', 'categoryItemCode', 'categoryItemName', 'note'));
        return $this->fetchAll($query);
    }
    public function checkCategory($category)
    {
        $category = (int)($category);
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p' => $this->_name), array('p.categoryItemName'))
                        ->joinLeft(array('i'=>'items'), 'p.categoryItemId = i.category',array())
                        ->where('i.category = ?',$category)
                        ->group('p.categoryItemName');
                        
        return $this->fetchAll($query);
    }
    public function deleteData ($id)
    {
        $this->delete( 'categoryItemId = ' . (int) $id );
    }
    public function addData ($code, $name,$note = '', $createUser = 1)
    {
        $data = array ('categoryItemCode' => $code, 'categoryItemName' => $name, 'note' => $note,  'createUser' => $createUser, 'createDate' => date('Y-m-d H:i:s'));
        return $this->insert( $data );
    }
    public function getDataByID($id)
    {
        $id = (int) $id;
        $query = $this->select();
        $query->from($this->_name,array('categoryItemId', 'categoryItemCode', 'categoryItemName','note'));
        $query->where('categoryItemId = ?', $id);
        
        return $this->fetchRow($query);
    }
    public function updateData ($catId, $catCode, $catName, $note)
    {
        $data = array ( 'categoryItemCode' => $catCode,'categoryItemName' => $catName,'note' => $note );
        $this->update( $data, 'categoryItemId = ' . (int) $catId );
    }
    
}

























